/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.

    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/

    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews

    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/
package gpsnodelocator;

import java.util.Hashtable;

import org.knopflerfish.service.log.LogRef;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

public class Activator implements BundleActivator, EventHandler 
{
	private static final String CURRENT_LAT_LONG = "currentLatLong";
	private static final String CURRENT_ROUTE = "currentRoute";
	private static final String LAST_NODE ="lastNode";
	private static final String LATITUDE = "latitude";
	private static final String LONGITUDE = "longitude";
	public static BundleContext bc = null;	
	private GPSNodeLocatorConfig config;
	private  LogRef log;
	private ServiceRegistration serviceRegistration; 
	private GPSNodeLocator gpsNodeLocator;
	
	public void start(BundleContext bc) throws Exception
	{
		Activator.bc = bc;
		log = new LogRef(bc);
		try
		{
			config = GPSNodeLocatorConfig.getConfig();
		}
		catch(Exception e)
		{
			log.error(Activator.bc.getBundle().getHeaders().get(Constants.BUNDLE_NAME)+ " failed to start due to configuration file setup error.");
			System.err.println(Activator.bc.getBundle().getHeaders().get(Constants.BUNDLE_NAME)+ " failed to start due to configuration file setup error.");
			Activator.bc = null;
			throw e;
		}
		ServiceReference reference = bc.getServiceReference (EventAdmin.class.getName());
		EventAdmin eventAdmin = (EventAdmin)bc.getService(reference);
		Hashtable ht = new Hashtable();
		ht.put(EventConstants.EVENT_TOPIC, config.getIncomingEvents());
		serviceRegistration = bc.registerService(EventHandler.class.getName(), this, ht);
		gpsNodeLocator = new GPSNodeLocator(eventAdmin);
		System.out.println(bc.getBundle().getHeaders().get(Constants.BUNDLE_NAME)+ " started...");
	}
	
	public synchronized void handleEvent(Event event) 
	{
		try
		{
			String eventTopic = event.getTopic().trim();
			if (eventTopic.equals(CURRENT_LAT_LONG))
			{
				String currentLatitude = (String) event.getProperty(LATITUDE);
				String currentLongitude = (String) event.getProperty(LONGITUDE);
				//System.out.println("Received Current lat-long at GPSNodeLocator bundle"+currentLatitude + "," +currentLongitude);
				gpsNodeLocator.locateAndPublishCurrentNode(currentLatitude, currentLongitude);
			}
			else if (eventTopic.equals(CURRENT_ROUTE))
			{
				String currentRouteCode = (String) event.getProperty(CURRENT_ROUTE);
				gpsNodeLocator.setCurrentRouteCode(currentRouteCode);
				System.out.println("Received Current route at GPSNodeLocator bundle "+currentRouteCode);
			}
			else if (eventTopic.equals(LAST_NODE))
			{
				String lastNode = (String) event.getProperty(LAST_NODE);
				gpsNodeLocator.setLastNode(lastNode);
				System.out.println("Received lastNode at GPSNodeLocator bundle "+lastNode);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public void stop(BundleContext context) throws Exception 
	{
		if (serviceRegistration!=null)
		{
			serviceRegistration.unregister();
			serviceRegistration = null;
		}
		System.out.println(bc.getBundle().getHeaders().get(Constants.BUNDLE_NAME)+ " stopped!");
		Activator.bc = null;
	}
}
